package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import android.widget.Toast
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.GoodsDetailAdapter
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.api.bean.ShopingBean
import client.comm.baoding.databinding.ActivityGoodsdetailBinding
import client.comm.baoding.dialog.SelectSkuDialog
import client.comm.baoding.receiver.MeBroadcast
import client.comm.baoding.ui.vm.GoodsDetailModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.api.bean.UserInfo
import client.comm.commlib.utils.StoreUtil

class GoodsDetailActivity : BaseMiddleActivity<GoodsDetailModel, ActivityGoodsdetailBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_goodsdetail
    }

    val adapter: GoodsDetailAdapter by lazy {
        GoodsDetailAdapter(this, mImgLoader)
    }

    override fun initViewModel(): GoodsDetailModel = createViewModel()

    override fun initView() {
        binding.vm = viewModel
        binding.recyclerView.adapter = adapter
        binding.event = this

        binding.emptyLayout.setOnClickListener {
            viewModel.holdCall(
                intent.getStringExtra("goodsId")!!,
            )
        }
        viewModel.holdCall(
            intent.getStringExtra("goodsId")!!,
        )

    }

    override fun observerUI() {

        viewModel.goodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    it.data?.let {
                        if (it.detail_list != null && it.detail_list.size > 0)
                            adapter.setData(it.detail_list as MutableList<GoodsDetail.Detail?>, it)
                        else {
                            adapter.setData(ArrayList<GoodsDetail.Detail?>(), it)
                        }
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (it.code == 400) {
                        finish()
                    }
                    toast(it.message)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("加入购物车成功")

                } else {
                    Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
//                    toast("添加失败")
                }
            }
        })
    }

    fun ljgm() {
        if (viewModel.isShowing.value!!)
            return
        SelectSkuDialog(this).apply {
            id = viewModel.goodsDetail.value!!.data!!.goods_id
            price = viewModel.goodsDetail.value!!.data!!.price
            lpq_price = viewModel.goodsDetail.value!!.data!!.lpq_price
            integral = viewModel.goodsDetail.value!!.data!!.integral

            imgUrl = "${viewModel.goodsDetail.value!!.data!!.goods_img}"
            name = "${viewModel.goodsDetail.value!!.data!!.name}"
            ok = {
                var num = 0

                try {
                    num = it.toInt()
                } catch (e: Exception) {
                }

                if (num > 0) {
                    val list = ArrayList<ShopingBean>()
                    val bean = ShopingBean(
                        goods_id = id,
                        goods_img = imgUrl,
                        name = name,
                        old_price = viewModel.goodsDetail.value!!.data!!.old_price,
                        price = viewModel.goodsDetail.value!!.data!!.price,
                        lpq_price = viewModel.goodsDetail.value!!.data!!.lpq_price,
                        shopping_id = "",
                        store_id = "",
                        num = it.toInt(),
                        integral = viewModel.goodsDetail.value!!.data!!.integral,
//                        integral_f = viewModel.goodsDetail.value!!.data!!.integral_f,
                        price_r = viewModel.goodsDetail.value!!.data!!.price_r,
                        create_time = viewModel.goodsDetail.value!!.data!!.create_time,
                        sku = viewModel.goodsDetail.value!!.data!!.sku,
                    )
                    list.add(bean)
                    startActivity(
                        Intent(
                            this@GoodsDetailActivity,
                            ConfirmOrderActivity::class.java
                        ).apply {
                            putExtra("list", list)
                        })
                    finish()
                } else {
                    toast("请输入数量")
                }


            }
        }.show()
    }

    fun addCart() {
        if (viewModel.goodsDetail.value!!.data!!.lpq_price > 0) {
            toast("体验专区不能加入购物车")
            return
        }

        if (viewModel.isShowing.value!!)
            return

        SelectSkuDialog(this).apply {
            id = viewModel.goodsDetail.value!!.data!!.goods_id

            price = viewModel.goodsDetail.value!!.data!!.price
            lpq_price = viewModel.goodsDetail.value!!.data!!.lpq_price
            integral = viewModel.goodsDetail.value!!.data!!.integral
            imgUrl = "${viewModel.goodsDetail.value!!.data!!.goods_img}"
            name = "${viewModel.goodsDetail.value!!.data!!.name}"
            ok = {

                var num = 0

                try {
                    num = it.toInt()
                } catch (e: Exception) {
                }

                if (num > 0) {
                    viewModel.add_shopping_cart(id, it)
                } else {
                    toast("请输入数量")
                }

            }
        }.show()
    }

    fun goCart() {
//        startActivity(Intent(this, BuyCartActivity::class.java))
        sendBroadcast(Intent(MeBroadcast.MAIN_SWITCH_TAB).apply {
            putExtra("tabIndex", 2)
        })
        finish()
    }
}